package com.lagou.dao.impl;

import com.lagou.dao.CourseLessonDao;
import com.lagou.pojo.Course_Lesson;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;

public class CourseLessonImpl implements CourseLessonDao {
    /**
     * 保存课时信息
     * @param course_lesson 课时信息
     * @return
     */
    @Override
    public int saveLesson(Course_Lesson course_lesson) {
        try {
            //1.创建QueryRunner
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            //2.编写SQL
            String sql = "INSERT INTO course_lesson(`id`,`course_id`,`section_id`,`theme`,`duration`,`is_free`,`order_num`,create_time,update_time) VALUES (NULL,?,?,?,?,?,?,?,?);";

            //3.准备参数
            Object[] param = {course_lesson.getCourse_id(),course_lesson.getSection_id(),course_lesson.getTheme(),course_lesson.getDuration(),
                    course_lesson.getIs_free(),course_lesson.getOrder_num(),course_lesson.getCreate_time(),course_lesson.getUpdate_time()};

            //4.执行插入操作
            int row = qr.update(sql, param);

            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 修改课时信息
     * @param course_lesson 课时信息
     * @return
     */
    @Override
    public int updateLesson(Course_Lesson course_lesson) {

        try {
            //1.创建QueryRunner
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            //2.编写SQL
            String sql="UPDATE `course_lesson`\n" +
                    "SET\n" +
                    "  `course_id` = ?,\n" +
                    "  `section_id` = ?,\n" +
                    "  `theme` = ?,\n" +
                    "  `duration` = ?,\n" +
                    "  `is_free` = ?,\n" +
                    "  `order_num` = ?,\n" +
                    "  `update_time`=? \n"+
             "      WHERE `id` = ?;";
            //3.准备参数
            Object[] param = {course_lesson.getCourse_id(),course_lesson.getSection_id(),course_lesson.getTheme(),course_lesson.getDuration(),
                    course_lesson.getIs_free(),course_lesson.getOrder_num(),course_lesson.getUpdate_time(),course_lesson.getId()};
            //4.执行修改操作
            int row = qr.update(sql, param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 修改课程状态
     * @param lessonId 课程ID
     * @param status 状态
     * @return
     */
    @Override
    public int updateLessonStatus(int lessonId, int status) {
        try {
            //1.创建QueryRunner
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            //2.编写SQL
            String sql="UPDATE `course_lesson` SET `status` = ?  WHERE `id` = ?;";
            //3.准备参数
            Object[] param = {status,lessonId};
            //4.执行修改操作
            int row = qr.update(sql, param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
